var prefix = "/cms/expense"

$().ready(function() {
  validateRule();
});

$.validator.setDefaults({
  submitHandler : function() {
    save();
  }
});
function save() {
  $.ajax({
    cache : true,
    type : "POST",
    url : prefix + "/save",
    data : $('#signupForm').serialize(),// 你的formid
    async : false,
    error : function(request) {
      parent.layer.alert("Connection error");
    },
    success : function(data) {
      if (data.code == 0) {
        parent.layer.msg("操作成功");
        parent.reLoad();
        var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
        parent.layer.close(index);

      } else {
        parent.layer.alert(data.msg)
      }

    }
  });

}
function validateRule() {
  var icon = "<i class='fa fa-times-circle'></i> ";
  $("#signupForm").validate({
    rules : {
      content : {
        required : true
      },
      budget: {
        required: true,
        minNumber: $("#budget").val()
      },
      operator: {
        required: true,
        rangelength:[2,4]
      }
    },
    messages : {
      content : {
        required : icon + "请输入 费用明细"
      },
      budget : {
        required : icon + "请输入 收支金额",
        number: icon + "请输入 正确的金额"
      },
      operator : {
        required : icon + "请输入 经办人",
        rangelength: icon + "请输入长度在 2 到 4 个字之间的姓名"
      },
    }
  })

  //自定义validate验证输入的数字小数点位数不能大于两位
  jQuery.validator.addMethod("minNumber",function(value, element){
    var returnVal = true;
    var inputZ = value;
    var ArrMen = inputZ.split(".");    //截取字符串
    if(ArrMen.length == 2) {
      if(ArrMen[1].length > 2) {    //判断小数点后面的字符串长度
        returnVal = false;
        return false;
      }
    }
    return returnVal;
  },icon + "金额最多保留两位小数点");         //验证错误信息
}
